﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;

namespace Waker.Common
{
    public class ControlUtil
    {
        public static void BindData(Control.ControlCollection control, DataTable dt)
        {
            if (dt == null)
            {
                return;
            }
            foreach (Control ctl in control)
            {
                string name = ctl.Name;
                if (string.IsNullOrEmpty(name) || name.StartsWith("label"))
                {
                    continue;
                }
                string dataMember = name.Substring(3);
                if (dt.Columns.Contains(dataMember))
                {
                    ctl.DataBindings.Clear();
                    if (name.StartsWith("txt"))
                    {
                        ctl.DataBindings.Add("Text", dt, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
                    }
                    else
                    {
                        //如果数据源包含此字段才绑定，以防写错。
                        ctl.DataBindings.Add("EditValue", dt, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
                    }

                }
                BindData(ctl.Controls, dt);
            }
        }
        public static void BindData(Control.ControlCollection control, DataRow dr)
        {
            if (dr == null || dr.Table.Columns.Count < 1)
            {
                return;
            }
            //var dt= dr.Table.Clone();
            //dt.ImportRow(dr); 
            var dt = dr.Table;
            var index = dr.Table.Rows.IndexOf(dr);
            foreach (Control ctl in control)
            {
                string name = ctl.Name;
                if (string.IsNullOrEmpty(name) || name.StartsWith("label"))
                {
                    continue;
                }
                string dataMember = name.Substring(3);
                if (dr.Table.Columns.Contains(dataMember))
                {
                    ctl.BindingContext[dt].Position = index;
                    ctl.DataBindings.Clear();
                    if (name.StartsWith("txt"))
                    {
                        ctl.DataBindings.Add("Text", dt, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
                        //ctl.Text = dr[dataMember] == DBNull.Value ? string.Empty : dr[dataMember].ToString(); 
                    }
                    else
                    {
                        //如果数据源包含此字段才绑定，以防写错。
                        ctl.DataBindings.Add("EditValue", dt, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
                        //ctl = dr[dataMember] == DBNull.Value ? string.Empty : dr[dataMember].ToString();
                    }
                }
                BindData(ctl.Controls, dt);
            }
        }

        /// <summary>
        /// 绑定LookUpEdit通用方法
        /// </summary>
        public static void FillLookUpEdit(DataTable dt, DevExpress.XtraEditors.LookUpEdit lookUp, string displayMember, string valueMember)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                //绑定数据源
                lookUp.Properties.DataSource = dt.DefaultView;
                //显示的字段
                lookUp.Properties.DisplayMember = displayMember;
                //值字段
                lookUp.Properties.ValueMember = valueMember;

            }
        }

        /// <summary>
        /// 绑定LookUpEdit通用方法
        /// </summary>
        public static void FillLookUpEditByEmptyText(DataTable dt, DevExpress.XtraEditors.LookUpEdit lookUp, string displayMember, string valueMember)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr[displayMember] = "";
                dr[valueMember] = "";
                dt.Rows.Add(dr);

                //绑定数据源
                lookUp.Properties.DataSource = dt.DefaultView;
                //显示的字段
                lookUp.Properties.DisplayMember = displayMember;
                //值字段
                lookUp.Properties.ValueMember = valueMember;

            }
        }


        /// <summary>
        /// 绑定ComboBox通用方法
        /// </summary>
        public static void FillComboBox(DataTable dt, DevExpress.XtraEditors.ComboBoxEdit cmb, string fieldName)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    cmb.Properties.Items.Add(dr[fieldName]);
                }
            }
        }
        /// <summary>
        /// 绑定ImageComboBox
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ci"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public static void FillImageComboBox(DataTable dt, ImageComboBoxEdit ci, string displayMember, string valueMember)
        {
            FillImageComboBox(dt, ci, displayMember, valueMember, "");
        }

        public static void FillImageComboBox(DataTable dt, ImageComboBoxEdit ci, string displayMember, string valueMember, bool nullItem)
        {
            FillImageComboBox(dt, ci, displayMember, valueMember, "");
            if (nullItem)
            {
                ci.Properties.Items.Insert(0, new ImageComboBoxItem("", "", 0));
            }
        }

        public static void FillImageComboBox(DataTable dt, ImageComboBoxEdit ci, string displayMember, string valueMember, string IS_Default)
        {
            ci.Properties.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                int defaultIndex = -1;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (IS_Default != "")
                    {
                        if (dt.Rows[i][IS_Default] != DBNull.Value && Convert.ToBoolean(dt.Rows[i][IS_Default]) == true)
                        {
                            defaultIndex = i;
                        }
                    }
                    if (dt.Columns[valueMember].DataType == typeof(int))
                        ci.Properties.Items.Add(new ImageComboBoxItem(dt.Rows[i][displayMember].ToString(), Convert.ToInt32(dt.Rows[i][valueMember]), i));
                    else
                        ci.Properties.Items.Add(new ImageComboBoxItem(dt.Rows[i][displayMember].ToString(), dt.Rows[i][valueMember].ToString(), i));
                }
                ci.SelectedIndex = defaultIndex;
            }
        }

        public static void FillImageComboBox(DataTable dt, RepositoryItemImageComboBox ci, string displayMember, string valueMember)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Columns[valueMember].DataType == typeof(int))
                        ci.Items.Add(new ImageComboBoxItem(dt.Rows[i][displayMember].ToString(), Convert.ToInt32(dt.Rows[i][valueMember]), i));
                    else
                        ci.Items.Add(new ImageComboBoxItem(dt.Rows[i][displayMember].ToString(), dt.Rows[i][valueMember].ToString(), i));
                }
            }
        }

        public static void FillCheckedComboBox(DataTable dt, RepositoryItemCheckedComboBoxEdit ci, string displayMember, string valueMember)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ci.Items.Add(new CheckedListBoxItem(dt.Rows[i][valueMember].ToString(), dt.Rows[i][displayMember].ToString(), CheckState.Unchecked));

                }
            }
        }

        public static void FillCheckedComboBox(DataTable dt, CheckedComboBoxEdit ci, string displayMember, string valueMember)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ci.Properties.Items.Add(new CheckedListBoxItem(dt.Rows[i][valueMember].ToString(), dt.Rows[i][displayMember].ToString(), CheckState.Unchecked));

                }
            }
        }


    }
}
